package com.example.anew.journey.mynewjourney.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.bean.HistoricalDataViewBean;
import com.example.anew.journey.mynewjourney.utils.MyDpToPxUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/3/20
 */

public class MyHistoricalDataCustomView extends View {
    private int YoffSet = 60;  //y轴的偏移量
    private int controlswidth; //控件宽
    private int controlsheight; //控件高
    private Paint spaint1;
    private Paint scopepaint;

    public List<HistoricalDataViewBean> getMlist() {
        return mlist;
    }

    public void setMlist(List<HistoricalDataViewBean> tlist) {
        mlist.addAll(tlist);
        requestLayout();
        invalidate();
    }

    private List<HistoricalDataViewBean> mlist = new ArrayList();
    private int XoffSet = 100; //x轴偏移量
    private Context mcontext;
    private Paint mpaint;
    private int VerticalGridBgColor = 0xff666666; //格子垂直画笔颜色
    private int HeightPerScale;
    private Paint spaint;
    private int XinformationColor = 0xff323232; //x轴信息的颜色
    private Paint bpaint;
    private int rokenLinebcolor = 0xffFF0000; //折线画笔颜色

    public MyHistoricalDataCustomView(Context context) {
        this(context, null);
    }

    public MyHistoricalDataCustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyHistoricalDataCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mcontext = context;
        init();
    }

    private void init() {
        // 画背景的画笔
        mpaint = new Paint();
        mpaint.setAntiAlias(true);
        mpaint.setStyle(Paint.Style.STROKE);
        mpaint.setColor(VerticalGridBgColor);
        mpaint.setStrokeWidth(0.5f);

        //画坐标轴信息的画笔
        spaint = new Paint();
        spaint.setAntiAlias(true);
        spaint.setStyle(Paint.Style.STROKE);
        spaint.setColor(XinformationColor);
        spaint.setTextSize(20);

        //折现画笔
        bpaint = new Paint();
        bpaint.setAntiAlias(true);
        bpaint.setStyle(Paint.Style.STROKE);
        bpaint.setStrokeWidth(2);
        bpaint.setColor(rokenLinebcolor);

        scopepaint = new Paint();
        scopepaint.setAntiAlias(true);
        scopepaint.setStyle(Paint.Style.FILL);
        scopepaint.setColor(0x22666666);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       int  width =  MeasureSpec.getSize(widthMeasureSpec);
         controlsheight =  MeasureSpec.getSize(heightMeasureSpec);
        if(mlist.size()*XoffSet < MyDpToPxUtils.dp2px(mcontext,400)){
            controlswidth = MyDpToPxUtils.dp2px(mcontext,400) +MyDpToPxUtils.dp2px(mcontext,20);
        }else{
            controlswidth = mlist.size()*XoffSet + MyDpToPxUtils.dp2px(mcontext,20);
        }
        HeightPerScale = (controlsheight-(2*YoffSet))/4;
        setMeasuredDimension(controlswidth,controlswidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //todo 画垂直背景格子
        drawVerticalGridBg(canvas);
        // todo 画水平背景格子
        drawLevelGridBg(canvas);
        //todo  画范围
        drawScope(canvas);
        //todo 画折线
        drawBrokenLine(canvas);

    }
    //画范围
    private void drawScope(Canvas canvas) {
        canvas.drawRect(0,controlsheight-(YoffSet+HeightPerScale*3),controlswidth-MyDpToPxUtils.dp2px(mcontext,10),controlsheight-(YoffSet+HeightPerScale*1),scopepaint);
    }

    private void drawBrokenLine(Canvas canvas) {
        for (int i = 1; i < mlist.size(); i++) {
            float heighteachvalue  =  (float) (controlsheight - 2 * YoffSet) / 80;

            int  beforeYdate = (mlist.get(i - 1).getYvalue()-100) > 80 ? 80:(mlist.get(i - 1).getYvalue()-100);
            int afterValue = (mlist.get(i).getYvalue()-100) > 80? 80:(mlist.get(i).getYvalue()-100);

            beforeYdate = (mlist.get(i - 1).getYvalue()-100)> 0 ?(mlist.get(i - 1).getYvalue()-100):0;
            afterValue = (mlist.get(i).getYvalue()-100)> 0 ?(mlist.get(i).getYvalue()-100):0;
            if(beforeYdate > 80)  beforeYdate = 80;
            if(afterValue >80) afterValue = 80;
            canvas.drawLine(MyDpToPxUtils.dp2px(mcontext,60)+XoffSet*(i-1), controlsheight - (YoffSet + heighteachvalue *beforeYdate ),MyDpToPxUtils.dp2px(mcontext,60)+XoffSet*(i),controlsheight -( YoffSet + heighteachvalue *afterValue ),bpaint);
        }
    }

    private void drawLevelGridBg(Canvas canvas) {
        for (int i = 0; i < 5 ; i++) {
          // NLogger.e("index","==drawLevelGridBg== drawLevelGridBg drawLevelGridBg=="+(controlsheight-(YoffSet+HeightPerScale*i)));
            canvas.drawLine(0,controlsheight-(YoffSet+HeightPerScale*i),controlswidth-MyDpToPxUtils.dp2px(mcontext,10),controlsheight-(YoffSet+HeightPerScale*i),mpaint);
        }
    }

    private void drawVerticalGridBg(Canvas canvas) {
        for (int i = 0; i <controlswidth/XoffSet ; i++) {
           // NLogger.e("index","==drawLevelGridBg== drawLevelGridBg drawLevelGridBg=="+(MyDpToPxUtils.dp2px(mcontext,60)+XoffSet*i));
            canvas.drawLine(MyDpToPxUtils.dp2px(mcontext,60)+XoffSet*i,controlsheight -YoffSet,MyDpToPxUtils.dp2px(mcontext,60)+XoffSet*i,YoffSet,mpaint );
            if(i < mlist.size()){
                canvas.drawText(mlist.get(i).getDate(),MyDpToPxUtils.dp2px(mcontext,60)+XoffSet*i-MyDpToPxUtils.dp2px(mcontext,20),controlsheight -YoffSet/2,spaint);
            }
        }
    }
}
